package com.company.service.impl;

import com.company.common.Constants;
import com.company.model.result.CaptchaImageResult;
import com.company.service.ICacheService;
import com.company.service.ICaptchaService;
import com.company.utils.ImageCodeUtil;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by tu on 2017/7/4.
 */
@Service
public class CaptchaServiceImpl implements ICaptchaService {

  @Autowired
  ICacheService cacheService;

  @Override
  public CaptchaImageResult captcha() throws IOException {
    //生成验证码
    CaptchaImageResult result = ImageCodeUtil.createImageCode();
    String key = result.id + Constants.SALT_CAPTCHA_IMAGE;
    //添加缓存
    cacheService.setex(key, Constants.REDIS_EXTIME_CAPTCHA_IMAGE, result.getCaptcha());
    return result;
  }
}
